<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/page.php' );
require_once( '../../libs/new_smt_variables.php' );

if( $_SERVER['REQUEST_METHOD'] == 'GET'){

	$forms = $_GET;
	$category1 = $_GET["category1"];
	$category2 = $_GET["category2"];
	$category3 = $_GET["category3"];
	$category4 = $_GET["category4"];
	$name = $_GET["name"];
	$kana = $_GET["kana"];
	$email = $_GET["email"];
	$phone = $_GET["phone"];
	$delivery_date_1 = $_GET["delivery_date_1"];
	$delivery_date_2 = $_GET["delivery_date_2"];
	$m_date1 = $_GET["m_date_1"];
	$m_date2 = $_GET["m_date_2"];
	$pay_type1 = $_GET["pay_type1"];
	$pay_type2 = $_GET["pay_type2"];
	$pay_type3 = $_GET["pay_type3"];
	$pay_type4 = $_GET["pay_type4"];
	$o_status1 = $_GET["o_status1"];
	$o_status2 = $_GET["o_status2"];
	$m_status1 = $_GET["m_status1"];
	$m_status2 = $_GET["m_status2"];
	$delivery_order1 = $_GET["delivery_order1"];
	$delivery_order2 = $_GET["delivery_order2"];
	$email_status1 = $_GET["email_status1"];
	$email_status2 = $_GET["email_status2"];
	$sale_status1 = $_GET["sale_status1"];
	$sale_status2 = $_GET["sale_status2"];

	$current=1;
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}

	$where = '';
	$query = '';
	$category = array();
	if($category1 != null){
		$category[] = $category1;
		$query .="&category1=".$category1;
	}
	if($category2 != null){
		$category[] = $category2;
		$query .="&category2=".$category2;
	}
	if($category3 != null){
		$category[] = $category3;
		$query .="&category3=".$category3;
	}
	if($category4 != null){
		$category[] = $category4;
		$query .="&category4=".$category4;
	}
	if(count($category)>0){
		$where = $where." and category in (".implode(",", $category).")";
	}

	if($name != null){
		$where = $where." and name like '%".$name."%'";
		$query .="&name=".$name;
	}
	if($kana != null){
		$where = $where." and kana like '%".$kana."%'";
		$query .="&kana=".$kana;
	}
	if($email != null){
		$where = $where." and email like '%".$email."%'";
		$query .="&email=".$email;
	}

	if($phone != null){
		$where = $where." and phone like '%".$phone."%'";
		$query .="&phone=".$phone;
	}
	if($delivery_date_1 != null && $delivery_date_2 != null){
		$where = $where." and delivery_date BETWEEN '".$delivery_date_1."' and '".$delivery_date_2."'";
		$query .="&delivery_date_1=".$delivery_date_1."&delivery_date_2=".$delivery_date_2;
	}
	if($delivery_date_1 != null && $delivery_date_2 == null){
		$where = $where." and delivery_date >= '".$delivery_date_1."'";
		$query .="&delivery_date_1=".$delivery_date_1;
	}
	if($delivery_date_1 == null && $delivery_date_2 != null){
		$where = $where." and delivery_date <= '".$delivery_date_2."'";
		$query .="&delivery_date_2=".$delivery_date_2;
	}

	if($m_date1 != null && $m_date2 != null){
		$where = $where." and m_date BETWEEN '".$m_date1."' and '".$m_date2."'";
		$query .="&u_date1=".$m_date1."&u_date2=".$m_date2;
	}

	$pay_type = array();
	if($pay_type1 != null){
		$pay_type[] = $pay_type1;
		$query .="&pay_type1=".$pay_type1;
	}
	if($pay_type2 != null){
		$pay_type[] = $pay_type2;
		$query .="&pay_type2=".$pay_type2;
	}
	if($pay_type3 != null){
		$pay_type[] = $pay_type3;
		$query .="&pay_type3=".$pay_type3;
	}
	if($pay_type4 != null){
		$pay_type[] = $pay_type4;
		$query .="&pay_type4=".$pay_type4;
	}
	if(count($pay_type)>0){
		$where = $where." and pay_type in (".implode(",", $pay_type).")";
	}

	$o_status = array();
	if($o_status1 != null){
		$o_status[] = $o_status1;
		$query .="&o_status1=".$o_status1;
	}
	if($o_status2 != null){
		$o_status[] = $o_status2;
		$query .="&o_status2=".$o_status2;
	}
	if(count($o_status)>0){
		$where = $where." and o_status in (".implode(",", $o_status).")";
	}

	$m_status = array();
	if($m_status1 != null){
		$m_status[] = $m_status1;
		$query .="&m_status1=".$m_status1;
	}
	if($m_status2 != null){
		$m_status[] = $m_status2;
		$query .="&m_status2=".$m_status2;
	}
	if(count($m_status)>0){
		$where = $where." and m_status in (".implode(",", $m_status).")";
	}

	$delivery_order = array();
	if($delivery_order1 != null){
		$delivery_order[] = $delivery_order1;
		$query .="&delivery_order1=".$delivery_order1;
	}
	if($delivery_order2 != null){
		$delivery_order[] = $delivery_order2;
		$query .="&delivery_order2=".$delivery_order2;
	}
	if(count($delivery_order)>0){
		$where = $where." and delivery_order in (".implode(",", $delivery_order).")";
	}

	$email_status = array();
	if($email_status1 != null){
		$email_status[] = $email_status1;
		$query .="&email_status1=".$email_status1;
	}
	if($email_status2 != null){
		$email_status[] = $email_status2;
		$query .="&email_status2=".$email_status2;
	}
	if(count($email_status)>0){
		$where = $where." and email_status in (".implode(",", $email_status).")";
	}

	$sale_status = array();
	if($sale_status1 != null){
		$sale_status[] = $sale_status1;
		$query .="&sale_status1=".$sale_status1;
	}
	if($sale_status2 != null){
		$sale_status[] = $sale_status2;
		$query .="&sale_status2=".$sale_status2;
	}
	if(count($sale_status)>0){
		$where = $where." and sale_status in (".implode(",", $sale_status).")";
	}

	$sql =  "SELECT * FROM k_order_head WHERE 1 = 1 $where order by order_id desc  ";
	
	$db = new Class_DB;
	
	$head = "見積番号,受注日,受注ステータス,入金ステータス,納品書出力,発送メール送信,売上ステータス,種別,企業形態,氏名,フリガナ,メールアドレス,電話番号,FAX番号,郵便番号,住所,使用目的,納品日,納品先郵便番号,納品先住所,納品先電話番号,連絡手段,ご希望納期,プリント工房の利用,どこで知りました？,支払方法,特記事項,コメント,小計,値引き,値引き(掛け率),送料種類,送料,消費税,合計,備考,特価理由,登録日,更新日";
	
	$res = $db->query( $sql );
	
	$data = array();
	
	while ( $row = mysql_fetch_array ( $res, MYSQL_ASSOC ) ) {
	
		$pofusearray = unserialize($row['pofuse']);
		$pofuse = "";
		if($pofusearray[0]==1) $pofuse .= "作品展　";
		if($pofusearray[1]==2) $pofuse .= "コンテスト募集　";
		if($pofusearray[2]==3) $pofuse .= "個人所有や贈与品　";
		if($pofusearray[3]==4) $pofuse .= "その他:".$row['pofuse_info'];
		$row['pofuse']=$pofuse;
		
		$printusearray = unserialize($row['print_use']);
		$printuse = "";
		if($printusearray[0]==1) $printuse .= "初めて　";
		if($printusearray[0]==2) $printuse .= "利用したことがある　";
		if($printusearray[1]!='') $printuse .= $printusearray[1]."年　";
		if($printusearray[2]!='') $printuse .= $printusearray[2]."月頃　";
		$row['printuse']=$printuse;
		
		$wherefromarray = unserialize($row['wherefrom']);
		$wherefrominfoarray = unserialize($row['wherefrom_info']);
		$wherefrom = "";
		if($wherefromarray[0]==1) $wherefrom .= "HP　";
		if($wherefromarray[1]==2) $wherefrom .= "雑誌　";
		if($wherefromarray[2]==3) $wherefrom .= "友人紹介:".$wherefrominfoarray[0];
		if($wherefromarray[3]==4) $wherefrom .= "その他:".$wherefrominfoarray[1];
		$row['wherefrom']=$wherefrom;
		$row['special']=nl2br($row['special']);
		$row['remark']=nl2br($row['remark']);
		
		$row['m_date']=date('Y/m/d',strtotime($row['m_date']));
		$row['u_date']=date('Y/m/d',strtotime($row['u_date']));
		$row['o_date']=date('Y/m/d',strtotime($row['o_date']));
		$row['delivery_date']=$row['delivery_date']==NULL?"":date('Y/m/d',strtotime($row['delivery_date']));
		$row['hope_date']=$row['hope_date']==NULL?"":date('Y/m/d',strtotime($row['hope_date']));
		
		
		if($row['category']==1){
			$row['category'] = "プリント工房PRO";
		}else if($row['category']==2){
			$row['category']= "プリント工房LITE";
		}else if($row['category']==3){
			$row['category']= "名刺工房";
		}else if($row['category']==4){
			$row['category']= "アルバム工房";
		}
		
		if ($row['delivery_contact'] == 1 ){$row['delivery_contact']="電話";}
		else if ($row['delivery_contact'] == 2 ){$row['delivery_contact']="FAX";}
		else if ($row['delivery_contact'] == 3 ){$row['delivery_contact']="Eメール";}
		else if ($row['delivery_contact'] == 4 ){$row['delivery_contact']="その他:(".$row['delivery_contact_info'].")";}
		
		if($row['pay_type'] == 1 ){$row['pay_type']='事前振込み';}
		else if($row['pay_type'] == 2 ){$row['pay_type']='代金引換';}
		else if($row['pay_type'] == 3 ){$row['pay_type']='請求書払い';}
		else if($row['pay_type'] == 4 ){$row['pay_type']='現金払い';}
		
		
		$sql2 = "SELECT * FROM k_order_detail as a WHERE a.order_id='".$row['order_id']."'";
		$result2 = $db->query($sql2);
		$m1 = 0;
		while( $row2 = mysql_fetch_array($result2,MYSQL_ASSOC) ){
			$row2['money'] = $row2['refer_price']*$row2['quantity'];
			$m1 = $m1+$row2['money'];
		}
		$m2 = $m1 - $m1*$row['discount'];
		$m3 = $row['shipping'];
		$m4 = round(($m1*$row['discount']+$m3)*$row['tax'],0);
		$m5 = $m1-$m2+$m3+$m4;
		if(strpos($row['special'],"br")){
			$row['special']=" \"\"".$row['special']."\"\" ";
		}
		$row['special']=str_replace("<br />","\n",$row['special']);
		$row['special']=str_replace("<br/>","\n",$row['special']);
		
		if(strpos($row['remark'],"br")){
			$row['remark']=" \"\"".$row['remark']."\"\" ";
		}
		$row['remark']=str_replace("<br />","\n",$row['remark']);
		$row['remark']=str_replace("<br/>","\n",$row['remark']);
		
		$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
				$row['order_id'],
				$row['o_date'],
				$row['o_status']==0?"未確定":"確定",
				$row['m_status']==0?"未入金":"入金済",
				$row['delivery_order']==0?"未出力":"出力済",
				$row['email_status']==0?"未送信":"送信済",
				$row['sale_status']==0?"未確定":"確定",
				$row['category'],
				$row['comtype']==1?"法人":"個人",
				$row['name'],
				$row['kana'],
				$row['email'],
				$row['phone'],
				$row['fax'],
				$row['zip'],
				$row['address'],
				$row['pofuse'],
				$row['delivery_date'],
				$row['delivery_zip'],
				$row['delivery_address'],
				$row['delivery_phone'],
				$row['delivery_contact'],
				$row['hope_date'],
				$row['printuse'],
				$row['wherefrom'],
				$row['pay_type'],
				$row['special'],
				$row['remark'],
				$m1,
				$m2,
				$row['discount'],
				$delivery_menu[$m3],
				$m3,
				$m4,
				$m5,
				$row['quote_remark'],
				$row['quote_special'],
				$row['m_date'],
				$row['u_date']
		),
				"SJIS-win",
				"utf8"
		);
	}
	
	$filename = date("YmdHis") . '.csv';
	
	$head = mb_convert_encoding( $head, "SJIS-win", "utf8");
	
	
	header("Content-Type: application/octet-stream");
	header('Content-Disposition: attachment; filename=' . $filename );
	
	print $head;
	print "\n";
	print implode( $data, "\n" );

}



?>